package com.atguigu.tingshu.album.task;

import cn.hutool.core.collection.CollUtil;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 饭勺
 * @description 定时任务类
 * @createDate 2025/9/24 20:38
 */
@Slf4j
@Component
public class ReviewTask {
    @Autowired
    private TrackInfoMapper trackInfoMapper;

    @Autowired
    private AuditService auditService;

    /**
     * 定时任务,获取审核结果
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void getReviewResultTask() {
        log.info("开始获取审核结果");
        //1.查询状态为：审核中 声音列表
        List<TrackInfo> trackInfoList = trackInfoMapper.selectList(
                new LambdaQueryWrapper<TrackInfo>()
                        .eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_REVIEWING)
                        .select(TrackInfo::getId, TrackInfo::getReviewTaskId)
                        .last("limit 100")
        );
        //2.遍历获取声音审核结果，根据内容审核建议更新审核状态
        if (CollUtil.isNotEmpty(trackInfoList)) {
            for (TrackInfo trackInfo : trackInfoList) {
                log.info("执行定时任务,中根据审核任务ID：{}获取审核结果", trackInfo.getReviewTaskId());
                String suggestion = auditService.getReviewResult(trackInfo.getReviewTaskId());
                if (StringUtils.isNotBlank(suggestion)) {
                    if ("pass".equals(suggestion)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
                    } else if ("block".equals(suggestion)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_NO_PASS);
                    } else if ("review".equals(suggestion)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_REVIEW);
                    }
                    trackInfoMapper.updateById(trackInfo);
                }
            }
        }
    }


}
